﻿using System;
using System.Collections.Generic;
using System.Text;
using NLBS.Framework;
using System.Globalization;
using NLBS.Entity;

namespace NLBS.Data.Business
{
    public class ExtensionLinkData 
    {
        private static readonly string ExtensionLinkListKey = "extensionLinkKey{0}";
        private static readonly string ExtensionLinkKey = "extensionLink{0}";
        public static List<ExtensionLink> GetExtensionLink(int type)
        {
            string key = string.Format(ExtensionLinkListKey, type);
            ContentCache cache = ContentCache.Instantiate();
            List<ExtensionLink> lists = (List<ExtensionLink>)cache[key];
            if (lists == null)
            {
                lists = new List<ExtensionLink>(DbProvider.Instance().GetExtensionLink(type));
                if (lists != null)
                {
                    cache.Insert(key, lists, CacheDuration.Long);
                    foreach (ExtensionLink item in lists)
                    {
                        cache.Insert(string.Format(ExtensionLinkKey, item.LinkID), item, CacheDuration.Long);
                    }
                }
            }
           
            return lists;
        }


        public static List<ExtensionLink> GetExtensionLinkList(int type)
        {
            List<ExtensionLink> list = GetExtensionLink(type);
            list.Sort(Compare);
            return list;
        }

        public static ExtensionLink GetExtensionLinkByID(int id)
        {
            string key = string.Format(ExtensionLinkKey, id);
            ContentCache cache = ContentCache.Instantiate();
            ExtensionLink link = (ExtensionLink)cache[key];
            if (link == null)
            {
                link = DbProvider.Instance().GetExtensionLinkDetail(id);
                if (link != null)
                {
                    cache.Insert(key, link, CacheDuration.Long);
                }
            }
            return link;
        }

        public static void InsertExtensionLink(ExtensionLink link)
        {
            int result = DbProvider.Instance().InsertExtensionLink(link);
            link.LinkID = result;
            GetExtensionLink(link.Type).Add(link);
        }

        public static int Compare(ExtensionLink x, ExtensionLink y)
        {
            if (x == null)
            {
                if (y == null)
                {

                    return 0;
                }
                else
                {

                    return -1;
                }
            }
            else
            {

                if (y == null)
                {
                    return 1;
                }
                else
                {

                    int retval = x.Order.CompareTo(y.Order);

                    if (retval != 0)
                    {
                        return retval;
                    }
                    else
                    {
                        return x.Order.CompareTo(y.Order);
                    }
                }
            }
        }

        public static void UpdateExtensionLink(ExtensionLink link)
        {
            DbProvider.Instance().UpdateExtensionLink(link);
        }

        public static void DeleteExtensionLink(int linkid)
        {
            
            DbProvider.Instance().DeleteExtensionLink(linkid);
            List<ExtensionLink> list = GetExtensionLink(1);
            
            list.ForEach(delegate(ExtensionLink link)
            {
                if (link.LinkID == linkid)
                    list.Remove(link);
            });

        }
    }
}
